﻿Imports JPC.Medialibrary.UI
Imports JPC.Medialibrary.BLL

Partial Class Youtube
    Inherits System.Web.UI.Page

    Const swfUrl As String = "http://www.youtube.com/v/{0}&enablejsapi=1&playerapiid=ytplayer&autoplay=1&fs=1"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        System.Threading.Thread.Sleep(80)

        If Page.IsPostBack Then
            Breadcrumb.Bind(lblLastBreadcrumb, lstBreadcrumb)
            If Me.cmdName.Value <> "" Then
                ProcesarComando(Me.btnText.Value, Me.cmdName.Value, Me.cmdArg.Value)
                Me.cmdName.Value = ""
                Me.cmdArg.Value = ""
                Me.btnText.Value = ""
            End If
        Else
            MySession.BreadCrumbList = Nothing
            MySession.LastBreadcrumb = Nothing
            Breadcrumb.AddItem("Inicio", "Inicio", 0)
            Breadcrumb.Bind(lblLastBreadcrumb, lstBreadcrumb)
        End If

    End Sub

    Private Sub ProcesarComando(ByVal Text As String, ByVal CommandName As String, ByVal CommandArgument As String)

        Breadcrumb.AddItem(Text, CommandName, CommandArgument)
        Breadcrumb.Bind(lblLastBreadcrumb, lstBreadcrumb)

        Select Case CommandName
            Case "Inicio"
                Response.Redirect("~/Start.aspx")
            Case "GetVideosByText"
                GetVideosByText()
            Case "PlayVideo"
                Me.videoUrl.Value = CommandArgument
        End Select

    End Sub

    Private Sub GetVideosByText()

        If Me.txtSearch.Text.Trim <> "" Then
            Me.lstMain.DataSource = YouTubeManager.GetVideosByText(Me.txtSearch.Text.Trim)
            lstMain.DataBind()
        End If

    End Sub

    Protected Sub btnDefault_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDefault.Click
        ProcesarComando("Videos", "GetVideosByText", -1)
    End Sub

    Protected Sub lstMain_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lstMain.ItemDataBound

        If (e.Item.ItemType = ListViewItemType.DataItem) Then
            Dim Thumbnail As ImageButton = CType(e.Item.FindControl("imgThumbnail"), ImageButton)
            Dim Title As Label = CType(e.Item.FindControl("lblVideoTitle"), Label)
            Dim Description As Label = CType(e.Item.FindControl("lblVideoDescription"), Label)


            Dim dataItem As Object = CType(e.Item, ListViewDataItem).DataItem
            Dim li As ListItem = CType(dataItem, ListItem)

            Dim tituloVideo As String = li.Text
            If tituloVideo.Length > 40 Then
                tituloVideo = Mid(tituloVideo, 1, 40) & "..."
            End If

            Thumbnail.ImageUrl = li.ThumbnailURL
            Thumbnail.OnClientClick = "javascript:play_video_step_1('" & String.Format(swfUrl, li.CommandArgument) & "','" & tituloVideo & "');"
            Title.Text = li.Text
            Description.Text = li.Info

        End If

    End Sub

    Protected Sub lstBreadcrumb_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lstBreadcrumb.ItemDataBound

        Breadcrumb.ItemDataBound(sender, e)

    End Sub

    Protected Sub lstBreadcrumb_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles lstBreadcrumb.ItemCommand

        Dim index As Integer = CInt(e.CommandArgument)
        Dim li As ListItem = MySession.BreadCrumbList(index)
        Breadcrumb.RemoveRange(index)
        ProcesarComando(li.Text, li.CommandName, li.CommandArgument)

    End Sub

End Class
